const App = getApp();
const Dialog = require('../../components/dialog/dialog');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_read: false,
    disabled: false,
    submsgSetting: {}, // 订阅消息配置
  },
  // 获取广告
  fetchAdver(){
    App._post_form('info/getAdverts', {type:4},  (res) =>{
        console.log(res.data)
        this.setData({
            adverts:res.data.adverts
        })
      });
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData(options)
    let _this = this;
    // 获取分销商申请状态
    _this.getApplyState();
    if(options.type){

    }else{
      console.log(777777777777)
      // this.getteaminfo()
    }
   
  },
    // _this.fetchAdver()
    // 获取订阅消息配置
    // _this.getSubmsgSetting();
    
  

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  

  /**
   * 获取订阅消息配置
   */
  getSubmsgSetting() {
    let _this = this;
    App._get('wxapp.submsg/setting', {}, (result) => {
      _this.setData({
        submsgSetting: result.data.setting
      });
    });
  },
   /**
   * 获取我的团队信息
   */
  getteaminfo() {
    let _this = this;
    App._post_form('user.team/getMyInfoByTeam', {}, (result) => {
      if(result.data.is_has_team===1){
        wx.navigateTo({
          url: './home/index',
        })
      }
    });
  },

  /**
   * 获取团队长审核状态
   */
  getApplyState() {
    let _this = this;
    App._get('user.team/getTeamHeadApplyStatus', {
      referee_id: _this.getRefereeid()
    }, (result) => {
      if(result.data.status===1){
        wx.reLaunch({
          url: './home/index',
        })
      }
      this.setData(result.data)
    });
  },
  /**
   * 查看驳回原因
   */
  handleBoHui(){
    wx.showModal({
      title: '友情提示',
      content: this.data.bh_remark,
      showCancel: false,
      success(res) {
       
      }
    });
  },
  /**
   * 显示申请协议
   */
  toggleApplyLicense() {
    wx.navigateTo({
      url: './rights?type=1',
    })
  },

  /**
   * 已阅读
   */
  toggleSetRead() {
    let _this = this;
    _this.setData({
      is_read: !this.data.is_read
    });
  },

  /**
   * 提交申请 
   */
  onFormSubmit(e) {
    let _this = this
    // 验证是否阅读协议
    if (!_this.data.is_read) {
      App.showError('请先阅读分销商申请协议');
      return false;
    }

    // 按钮禁用
    _this.setData({
      disabled: true
    });

    // 数据提交
    App._post_form('user.team/applyHead', {}, (res) => {
    
      console.log(res)
      if(res.code==100){
        wx.showModal({
          title: '友情提示',
          content: res.msg,
          showCancel: false,
          success(res) {
            wx.navigateTo({
              url: '/pages/dealer/index/index',
            })
          }
        });
        return false;
      }
      App.showSuccess("申请成功")
      // 获取分销商申请状态
      _this.getApplyState();
    }, null, () => {
      // 解除按钮禁用
      _this.setData({
        disabled: false
      });
    });
  },

  /**
   * 去商城逛逛
   */
  navigationToIndex(e) {
    // 跳转到首页
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  /**
   * 订阅消息通知
   */
  onSubMsg() {
    let _this = this;
    let tmplItem = _this.data.submsgSetting.dealer.apply.template_id;
    if (tmplItem.length > 0) {
      wx.requestSubscribeMessage({
        tmplIds: [tmplItem],
        success(res) {},
        fail(res) {},
        complete(res) {},
      });
    }
  },

  /**
   * 获取推荐人id
   */
  getRefereeid() {
    return wx.getStorageSync('referee_id');
  },

})